package com.weblog.user.controller;

import com.sun.org.apache.bcel.internal.generic.SWITCH;
import com.weblog.model.article.output.ArticleCusVo;
import com.weblog.model.user.CusCategoryInfo;
import com.weblog.user.enums.ResultEnum;
import com.weblog.user.service.CusCategoryService;
import entity.Result;
import entity.ResultMessage;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2019/11/122:11
 */
@RestController
@RequestMapping("/cuscategory")
@CrossOrigin
public class CusCategoryController {

    @Autowired
    private CusCategoryService cusCategoryService;

    /**
     * 条件查询
     * @param searchMap 查询条件map集合
     * @return
     */
    @PostMapping("/search")
    public Result multiSearch(@RequestBody Map<String,String> searchMap){
        List<CusCategoryInfo> cusCategoryInfos = cusCategoryService.multiSearch(searchMap);
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,cusCategoryInfos);
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping
    public Result categoriesAll(){
        List<CusCategoryInfo> resultMaps = cusCategoryService.categoriesAll();
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,resultMaps);
    }

    /**
     * 彻底删除自定义分类
     * @param id 待删除分类id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")String id){
        ResultEnum result = cusCategoryService.delete(id);
        if (result == ResultEnum.OK){
         return new Result(true,StatusCode.OK,ResultMessage.DELETE_SUCCESS);
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.USER_ERROR);
        }
    }
    /**
     * 删除自定义分类到回收站
     * @param id 待删除分类id
     * @return Result
     */
    @PutMapping("/recycle/{id}")
    public Result toRecycle(@PathVariable("id")String id){
        ResultEnum result = cusCategoryService.recycle(id);
        if (result ==ResultEnum.OK){
            return new Result(true,StatusCode.OK,ResultMessage.DELETE_SUCCESS);
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.USER_ERROR);
        }
    }

    /**
     * 修改分类名
     * @param updateMap 修改实体
     * @return Result
     */
    @PutMapping("/name")
    public Result updateName(@RequestBody Map<String,String> updateMap){
        ResultEnum resultEnum = cusCategoryService.updateName(updateMap);

        switch (resultEnum){
            case ACCESS_ERROR:
                return new Result(false,StatusCode.ERROR,ResultMessage.USER_ERROR);
            case OK:
                return new Result(true,StatusCode.OK,ResultMessage.UPDATE_SUCCESS);
            case TARGET_NOT_FOUND:
                return new Result(false,StatusCode.ERROR,ResultMessage.CATEGORY_NOT_FOUND);
            default:
                return new Result(false,StatusCode.ERROR,"执行出错");
        }

    }
    /**
     * 修改分类前台展示状态
     * @param updateMap 修改实体
     * @return Result
     */
    @PutMapping("/display")
    public Result updateDisplay(@RequestBody Map<String,String> updateMap){
        ResultEnum resultEnum = cusCategoryService.updateDisplay(updateMap);

        switch (resultEnum){
            case ACCESS_ERROR:
                return new Result(false,StatusCode.ERROR,ResultMessage.USER_ERROR);
            case OK:
                return new Result(true,StatusCode.OK,ResultMessage.UPDATE_SUCCESS);
            case TARGET_EXISTEED:
                return new Result(false,StatusCode.ERROR,ResultMessage.CATEGORY_EXISTED);
            case TARGET_NOT_FOUND:
                return new Result(false,StatusCode.ERROR,ResultMessage.CATEGORY_NOT_FOUND);
            default:
                return new Result(false,StatusCode.ERROR,"执行出错");
        }

    }


    /**
     * 添加自定义分类
     * @param addMap 自定义分类map集
     * @return
     */
    @PostMapping
    public Result delete(@RequestBody Map<String,String> addMap){
        ResultEnum result = cusCategoryService.add(addMap);
        switch (result){
            case OK:
                return new Result(true,StatusCode.OK,ResultMessage.SAVE_SUCCESS);
            case TARGET_EXISTEED:
                return new Result(false,StatusCode.ERROR,ResultMessage.CATEGORY_EXISTED);
            case ACCESS_ERROR:
                return new Result(false,StatusCode.ACCESSERROR,ResultMessage.ACCESS_ERROR);
            default:
                return new Result(false,StatusCode.ERROR,"执行出错");
        }
    }


    /**
     * 根据分类id查询该分类的所有文章
     * @param userId 用户id
     * @param id 分类id
     * @return
     */
    @GetMapping("/articles/{userId}/{id}")
    public Result cucArticle(@PathVariable String userId,@PathVariable String id){
        List<ArticleCusVo> mapList = cusCategoryService.queryCucArticle(userId, id);
        return new Result(true,StatusCode.OK,ResultMessage.QUERY_SUCCESS,mapList);
    }


    /**
     * 单个删除分类的文章
     * @param cucId 分类id
     * @param aid 文章id
     * @return
     */
    @PutMapping("/remove/single/{cucId}/{aid}")
    public Result removeSingleArticle( @PathVariable String cucId, @PathVariable String aid){
        cusCategoryService.removeSingleArticle(cucId,aid);
        return new Result(true,StatusCode.OK,ResultMessage.DELETE_SUCCESS);
    }
    /**
     * 批量删除分类的文章
     * @param batchData 待删除数据
     * @return
     */
    @PutMapping("/batchRemove")
    public Result batchRemove(@RequestBody Map<String,Object> batchData){
        cusCategoryService.batchRemoveArticle(batchData);
        return new Result(true,StatusCode.OK,ResultMessage.DELETE_SUCCESS);
    }
    /**
     * 批量复制分类的文章到另一个分类中
     * @param copyData 待复制数据
     * @param currentid 当前操作的分类id
     * @return
     */
    @PutMapping("/batchCopy/{currentid}")
    public Result batchCopy(@RequestBody Map<String, Object> copyData, @PathVariable String currentid){
        ResultEnum resultEnum = cusCategoryService.batchCopyArticle(copyData, currentid);
        switch (resultEnum){
            case TARGET_SELF:
                return new Result(false, StatusCode.ERROR,"不可复制到同一个分类");
            case OK:
                return new Result(true,StatusCode.OK, ResultMessage.COPY_SUCCESS);
            case TARGET_EXISTEED:
                return new Result(false,StatusCode.ERROR,"目标分类已存在所选文章");
            default:
                return new Result(false,StatusCode.ERROR,"执行出错");
        }

    }

    /**
     * 批量移动
     * @param moveData
     * @return
     */
    @PostMapping("/batchMove")
    public Result batchMove(@RequestBody Map<String,Object> moveData){
        ResultEnum resultEnum =cusCategoryService.batchMove(moveData);
        switch (resultEnum){
            case TARGET_SELF:
                return new Result(false, StatusCode.ERROR,"不可移动到同一个分类");
            case OK:
                return new Result(true,StatusCode.OK, ResultMessage.MOVE_SUCCESS);
            case TARGET_EXISTEED:
                return new Result(false,StatusCode.ERROR,"目标分类已存在所选文章");
            default:
                return new Result(false,StatusCode.ERROR,"执行出错");
        }

    }

    /**
     * 批量添加
     * @param addData
     * @return
     */
    @PostMapping("/batchAdd")
    public Result batchAdd(@RequestBody Map<String,Object> addData){
        ResultEnum resultEnum=cusCategoryService.batchAdd(addData);
        if (resultEnum==ResultEnum.OK){
            return new Result(true,StatusCode.OK,ResultMessage.SAVE_SUCCESS);
        }
        return new Result(false,StatusCode.ERROR,ResultMessage.CATEGORY_NOT_FOUND);
    }
    /**
     * 根据分类id查询该分类的所有文章id
     * @param userId 用户id
     * @param id 分类id
     * @return
     */
    @GetMapping("/client/aids/{userId}/{id}")
    public Result articleInfo(@PathVariable String userId,@PathVariable String id){
        List<String> aids = cusCategoryService.queryAids(userId, id);
        return new Result(true,StatusCode.OK,ResultMessage.QUERY_SUCCESS,aids);
    }


}
